package
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.*;
	import flash.utils.getQualifiedClassName;
	
	/**	This class is used to display the target shapes 
	 */
	public class TargetMenu extends Sprite
	{
		// public constants
		public static const CHECK_WIDTH:int = 20, X_PADDING:int = 5, Y_PADDING:int = 5;
		// public vars
		public var WIDTH:Number;
		public var TYPES:Array;
			
		// private objects
		//private var txml:XML; // input xml
		private var parentPolygons:Array;
		private var targets:Array; // an array of the target targets
		private var targetsChecked:Array; // corresponds to targets, array of booleans
		private var _cover:Shape;
		// private variables
		private var initialMask:Boolean;
		
		/** The input should just be the item node of the xml document */
		public function TargetMenu(WIDTH:int, parentPolygons:Array, TYPES:Array, initialCover:Boolean = true)
		{
			//this.txml = txml;
			this.WIDTH = WIDTH;
			this.parentPolygons = parentPolygons;
			this.TYPES = TYPES;
			targets = new Array();
			targetsChecked = new Array();
			// go through each target in the xml and add a new display item
			var curx:Number = 0;
			var cury:Number = 0;
			for each (var t:ParentPolygon in parentPolygons)
			{
				if (targets.length < 8)
				{
					var target:Sprite = drawTargetDisplay(t);
					
					if (target != null)
					{
						if (targets.length == 5)
						{
							cury = this.height+Y_PADDING;
							curx = 0;
						}
						target.x = curx;
						target.y = cury;
						curx = target.x + target.width + X_PADDING;
					}	
				} else 
				{
					trace("No room for more target shapes, max 8, sorry");
				}
			}
			
			this.graphics.lineStyle(1, 0x333333);
			this.graphics.beginFill(0xAA0000);
			this.graphics.drawRect(0,0,WIDTH,this.height);
			this.graphics.endFill();
			
			_cover = new Shape();
			_cover.graphics.beginFill(0xAA0000);
			_cover.graphics.drawRect(0,0,this.width, this.height);
			_cover.graphics.endFill();
			if (initialCover)
			{
				this.addChild(_cover);
			}
		}
		
		/** This function draws a display for the given target, if allowable */
		public function drawTargetDisplay(parentPolygon:ParentPolygon):Sprite
		{
			var type:String;
			type = parentPolygon.type;
			if (TYPES.indexOf(type) > -1)
			{
				var tfield:TextField = new TextField();
				tfield.mouseEnabled = false;
				tfield.selectable = false;
				tfield.defaultTextFormat = new TextFormat("arial", 18, 0xFFFFFF);
				tfield.text = type.split("_").join(" ");
				tfield.autoSize = "left";
				var target:Sprite = new Sprite();
				target.name = type;
				target.graphics.lineStyle(1,0x333333);
				target.graphics.beginFill(0x666666);
				target.graphics.drawRect(0,0,tfield.width + CHECK_WIDTH + 12, Math.max(tfield.height, CHECK_WIDTH) + 6);
				target.graphics.endFill();
				
				tfield.x = 2;
				tfield.y = 2;
				target.addChild(tfield);
				
				var checkBox:Shape = new Shape();
				checkBox.graphics.lineStyle(2, 0x000000);
				checkBox.graphics.beginFill(0xFFFFFF);
				checkBox.graphics.drawRect(0,0,CHECK_WIDTH, CHECK_WIDTH);
				checkBox.graphics.endFill();
				checkBox.x = tfield.x + tfield.width + 4;
				checkBox.y = (target.height - checkBox.height)/2;
				target.addChild(checkBox);
								
				this.addChild(target);
				targets.push(target);
				targetsChecked.push(false);
				return target;
			}
			return null;
		}
		/**
		 * Gets whether a given type is checked or not.  Since there may be multiple of the same type skipCount allows you to skip over the first n shapes of the type.
		 * */
		public function getTypeCheck(type:String, skipCount:uint=0):Boolean
		{
			// find the right target
			var found:Boolean = false;
			for (var index:uint = 0; index < targets.length; index++)
			{
				if (Sprite(targets[index]).name == type)
				{
					if (skipCount==0)
					{
						found = true; break;
					} else
					{
						skipCount--;
					}
				}
			}
			if (found)
			{
				return Boolean(targetsChecked[index]);
			} else { return false; }
	
		}
		/** set to true to demark a completed shape, uncheck to demark an incopmlete shape.  Will address first of type by default, increase skipCount to change.	
		 *  Also, if the current flag setting on the found type already matches the input type, try the next.  
		 * e.g. if the first triangle is already checked, try the next triangle.		 *  
		 */
		public function setTypeCheck(type:String, b:Boolean, skipCount:uint=0):void
		{
			// find the right target
			var found:Boolean = false;
			for (var index:uint = 0; index < targets.length; index++)
			{
				if (Sprite(targets[index]).name == type)
				{
					// are we skipping this one (skipCount>0), 
					if (skipCount==0)
					{
						//is this one already set correctly
						if (targetsChecked[index] != b)
						{
							targetsChecked[index] = b;
							var target:Sprite = targets[index];
							// find the sprite, which is the check 
							for (var i:uint=0; i < target.numChildren; i++)
							{		
								//trace(flash.utils.getQualifiedClassName(target.getChildAt(i)));
								if (flash.utils.getQualifiedClassName(target.getChildAt(i)) == "flash.display::Shape")
								{
									var checkBox:Shape = Shape(target.getChildAt(i));
									
									if (b)
									{
										checkBox.graphics.lineStyle(2, 0x000000);
										checkBox.graphics.beginFill(0x88FF88);
										checkBox.graphics.drawRect(0,0,CHECK_WIDTH, CHECK_WIDTH);
										checkBox.graphics.endFill();
										checkBox.graphics.lineStyle(4, 0x006600);
										checkBox.graphics.moveTo(4, CHECK_WIDTH/2);
										checkBox.graphics.lineTo(CHECK_WIDTH/2, CHECK_WIDTH-4);
										checkBox.graphics.lineTo(CHECK_WIDTH - 4, 4);
									} else
									{
										checkBox.graphics.lineStyle(2, 0x000000);
										checkBox.graphics.beginFill(0xFFFFFF);
										checkBox.graphics.drawRect(0,0,CHECK_WIDTH, CHECK_WIDTH);
										checkBox.graphics.endFill();
									}
								}					
							}
							// since we found a shape, could be that we've found all of our shapes, check it out
							checksCompleted();
							return;
						}						
					} else
					{
						skipCount--;
					}
				}
			}
			
			
		}
		/** Checks to see if all checks have been checked. If so dispatches a COMPLETE and returns true, else returns false; */
		public function checksCompleted():Boolean
		{
			for each (var b:Boolean in targetsChecked)
			{
				if (!b) return false;
			}
			// all are checked
			this.dispatchEvent(new Event(Event.COMPLETE));
			return true;
		}
		/** Cover up names of shapes*/
		public function cover():void
		{
			if (_cover.parent != this)
			{
				this.addChild(_cover);
			}
		}
		/** Uncover up names of shapes*/
		public function uncover():void
		{
			if (_cover.parent == this)
			{
				this.removeChild(_cover);
			}
		}
	}
}